﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sockets2
{
    public partial class ClienteUI : Form
    {
        Cliente c = new Cliente();
        string Log { get { return txtLog.Text; } set { txtLog.Text += value + "\r\n"; } }
        string Recibido { set { txtRecibir.Text += value + "\r\n"; } }

        public ClienteUI()
        {
            InitializeComponent();
        }

        private void btnConectar_Click(object sender, EventArgs e)
        {
            //Iniciamos el Cliente
            Log = "Conectando";
            if (c.Conectar(txtIP.Text))
                Log = "Conectado";
            
        }

        private void btnEnviar_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (c.Conectado())
            {
                this.Text = "Estoy conectado";
                if (c.nuevoMsj())
                {
                    this.Text += "Tengo un mensaje";
                    Recibido = c.Recibir();
                }
                else
                {
                    this.Text += "no tengo nada";
                }
            }
            else
            {
                this.Text = "no lo estoy";
            }

        }

        private void txtRecibir_TextChanged(object sender, EventArgs e)
        {

        }

        private void ClienteUI_Load(object sender, EventArgs e)
        {

        }

        private void TxtEnviar_TextChanged(object sender, EventArgs e)
        {

        }

        private void TxtEnviar_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
            if (c.Conectado())
            {
                c.Enviar(TxtEnviar.Text);
                TxtEnviar.Text = "";
            }
            }
        }

        private void ClienteUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            c.Desconectar();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            c.RecibirArchivo();
        }

    }
}
